package com.syni.mdd.yhd.common.all.service.impl;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsVerRecordDao;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuyUse;
import com.syni.mdd.yhd.common.all.entity.BmsVerRecord;
import com.syni.mdd.yhd.common.all.entity.coupon.BmsUserReceCoupon;
import com.syni.mdd.yhd.common.all.service.BmsVerRecordService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
@Log4j2
public class BmsVerRecordServiceImpl extends BaseService<BmsVerRecord, Integer> implements BmsVerRecordService {

    @Autowired
    BmsVerRecordDao bmsVerRecordDao;

    @Override
    public BmsVerRecordDao getBaseDao() {
        return bmsVerRecordDao;
    }

    @Override
    public void addRecordByBuyQuan(List<BmsDxGroupBuyUse> bmsDxGroupBuyUses) {
        try {
            if(bmsDxGroupBuyUses.size() > 0){
                List<BmsVerRecord> bmsVerRecords = new ArrayList<>();
                for(BmsDxGroupBuyUse bmsDxGroupBuyUse : bmsDxGroupBuyUses){
                    BmsVerRecord bmsVerRecord = new BmsVerRecord();
                    bmsVerRecord.setVerId(UUID.randomUUID().toString().replaceAll("-",""));
                    bmsVerRecord.setVerCode(bmsDxGroupBuyUse.getGroupBuyCode());
                    bmsVerRecord.setOrderId(bmsDxGroupBuyUse.getOrderId());
                    bmsVerRecord.setVerType(Constants.VER_TYPE_MUY);
                    bmsVerRecord.setNewTime(new Date());
                    bmsVerRecords.add(bmsVerRecord);
                }
                if(bmsVerRecords.size() > 0){
                    bmsVerRecordDao.saveAll(bmsVerRecords);
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }

    @Override
    public List<BmsVerRecord> getRecodeByCodes(List<String> groupBuyUseCode) {
        if(groupBuyUseCode.size() > 0) {
            Map<String, Object> conditionMap = new HashMap<>();
            Set<String> codes = new HashSet<>(groupBuyUseCode);
            conditionMap.put("verCode_IN",codes);
            return bmsVerRecordDao.findAll(new SpecificationUtils(conditionMap));
        }
        return null;
    }

    public List<BmsVerRecord> getRecodeByCode(String groupBuyUseCode) {
        List<String> codes = new ArrayList<>();
        codes.add(groupBuyUseCode);
        return getRecodeByCodes(codes);
    }

    public List<BmsVerRecord> getRecodeByOrderId(Integer orderId) {
        Map<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("orderId",orderId);
        return bmsVerRecordDao.findAll(new SpecificationUtils(conditionMap));
    }

    public List<BmsVerRecord> getRecodeByReceId(Integer receId) {
        Map<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("receId",receId);
        return bmsVerRecordDao.findAll(new SpecificationUtils(conditionMap));
    }

    @Override
    public BmsVerRecord getRecodeByVerId(String verId) {
        Map<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("verId",verId);
        List<BmsVerRecord> bmsVerRecords = bmsVerRecordDao.findAll(new SpecificationUtils(conditionMap));
        if(CollectionUtils.isNotEmpty(bmsVerRecords)){
            return bmsVerRecords.get(0);
        }
        return null;
    }

    @Override
    public void addRecordByReceQuan(List<BmsUserReceCoupon> bmsUserReceCoupons) {
        try {
            if(bmsUserReceCoupons.size() > 0){
                List<BmsVerRecord> bmsVerRecords = new ArrayList<>();
                for(BmsUserReceCoupon bmsUserReceCoupon : bmsUserReceCoupons){
                    BmsVerRecord bmsVerRecord = new BmsVerRecord();
                    bmsVerRecord.setVerId(UUID.randomUUID().toString().replaceAll("-",""));
                    bmsVerRecord.setVerCode(bmsUserReceCoupon.getConsumerCode());
                    bmsVerRecord.setReceId(bmsUserReceCoupon.getId());
                    bmsVerRecord.setVerType(Constants.VER_TYPE_RECE);
                    bmsVerRecord.setNewTime(new Date());
                    bmsVerRecords.add(bmsVerRecord);
                }
                if(bmsVerRecords.size() > 0){
                    bmsVerRecordDao.saveAll(bmsVerRecords);
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }

    @Override
    public void addRecordByReceQuan(BmsUserReceCoupon bmsUserReceCoupon) {
        List<BmsUserReceCoupon> bmsUserReceCoupons = new ArrayList<>();
        bmsUserReceCoupons.add(bmsUserReceCoupon);
        addRecordByReceQuan(bmsUserReceCoupons);
    }


}
